﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Text;

namespace LiMinCRMPlugin.InterfaceDocking
{
    /// <summary>
    /// MES接口通用方法类
    /// </summary>
    /// <remarks>
    /// 功能： 
    /// 创建人：wzx
    /// 创建日期：2023-5-17
    /// 修改信息（1）：修改人，修改日期，修改内容
    /// </remarks>
    public static class Common
    {

        /// <summary>
        /// 日志
        /// </summary>
        public class InterfaceLog
        {
            #region 日志参数
            /// <summary>
            /// 数据编码
            /// </summary>
            public string FBILLNO;
            /// <summary>
            /// 数据内码
            /// </summary>
            public string FBILLID;
            /// <summary>
            /// 调用时间
            /// </summary>
            public DateTime FDATE;
            /// <summary>
            /// 接口名称
            /// </summary>
            public string FINTERNAME;
            /// <summary>
            /// 调用方式
            /// </summary>
            public string FInvokeType;
            /// <summary>
            /// 同步状态
            /// </summary>
            public string FSTATUS;
            /// <summary>
            /// 接口Token
            /// </summary>
            public string Ftoken;
            /// <summary>
            /// 请求报文
            /// </summary>
            public string FRequest;
            /// <summary>
            /// 返回报文
            /// </summary>
            public string FReturn;

            public string FRequsetData
            {
                get { return FRequest.Length > 2000 ? FRequest.Substring(0, 1999) : FRequest; }
                set { FRequest = value; }
            }
            public string FReturnData
            {
                get { return FReturn.Length > 2000 ? FReturn.Substring(0, 1999) : FReturn; }
                set { FReturn = value; }
            }

            #endregion

            #region 写日志方法
            public string WriteLogSql()
            {
                string sql = string.Format(@"/*dialect*/ exec CreateMesLog '{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}' ",
                    FBILLNO, FBILLID, FDATE, FINTERNAME, FInvokeType, FSTATUS, Ftoken, FRequsetData, FReturnData, FRequest, FReturn);
                return sql;
            }
            public string WriteCRMLogSql()
            {
                string sql = string.Format(@"/*dialect*/ exec CreateCRMLog '{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}' ",
                    FBILLNO, FBILLID, FDATE, FINTERNAME, FInvokeType, FSTATUS, Ftoken, FRequsetData, FReturnData, FRequest, FReturn);
                return sql;
            }

            #endregion

        }
        /// <summary>
        /// POST请求返回json
        /// </summary>
        /// <param name="postDataStr">json数据</param>
        /// <returns></returns>
        public class RHeader
        {
            public string code { get; set; }
            public string message { get; set; }
        }
        public class RBody
        {
            public string handelTime { get; set; }
            public string formId { get; set; }
        }


        /// <summary>
        /// http请求返回json
        /// </summary>
        /// <param name="postDataStr">json数据</param>
        /// <returns></returns>
        public static string HttpPostEncode(string postDataStr, string HttpUrl)
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(HttpUrl);
            request.Method = "POST";//application/json
            request.ContentType = "application/json; charset=utf-8";

            byte[] bs = Encoding.UTF8.GetBytes(postDataStr);

            request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)";
            Stream myRequestStream = request.GetRequestStream();
            myRequestStream.Write(bs, 0, bs.Length);
            myRequestStream.Close();

            HttpWebResponse response = null;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (System.Exception ex)
            {
                return ex.Message.ToString();
            }
            if (response != null)
            {
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();

                return retString;  //输出并转换小写
            }
            else
            {
                return "error"; //post请求返回为空
            }
        }



        public enum LogResult
        {
            账号未激活 = -7,
            云通行证未绑定Cloud账号 = -6,
            需要表单处理 = -5,
            登录警告 = -4,
            密码验证不通过强制的 = -3,
            密码验证不通过可选的 = -2,
            登录失败 = -1,
            用户或密码错误 = 0,
            登录成功 = 1
        }

        /// <summary>
        /// API返回数据
        /// </summary>
        public class ResultObject
        {
            public Result result { get; set; }
        }
        public class Result
        {
            public ResponseStatus responseStatus { get; set; }
            public string id { get; set; }
            public string number { get; set; }
            public NeedReturnData[] needReturnData { get; set; }
        }
        public class ResponseStatus
        {
            public string IsSuccess { get; set; }
            public string ErrorCode { get; set; }
            public Errors[] Errors;
            public SuccessEntitys[] successEntitys { get; set; }
            public SuccessMessages[] successMessages { get; set; }
            public string MsgCode { get; set; }
        }
        public class SuccessEntitys
        {
            public string id { get; set; }
            public string Number { get; set; }
            public string DIndex { get; set; }
        }
        public class Errors
        {
            public string FieldName { get; set; }
            public string Message { get; set; }
            public string DIndex { get; set; }
        }
        public class SuccessMessages
        {
            public string FieldName { get; set; }
            public string Message { get; set; }
            public string DIndex { get; set; }
        }
        public class NeedReturnData
        {

        }
    }
}
